<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>珠峰架构师成长计划</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            珠峰架构师成长计划
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li class="active"><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-front.html">30.cms-10-front</a></li><li><a href="../html/30.cms-11-deploy.html">30.cms-11-deploy</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/54.linux.html">54.linux</a></li><li><a href="../html/55.yaml.html">55.yaml</a></li><li><a href="../html/50.ketang2.html">50.ketang2</a></li><li><a href="../html/56.ts.html">56.ts</a></li><li><a href="../html/57.ts_react.html">57.ts_react</a></li><li><a href="../html/58.react-ssr.html">58.react-ssr</a></li><li><a href="../html/59.react-ssr.html">59.react-ssr</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. Redux中间件">1. Redux中间件</a></li><li><a href="#t12. redux">2. redux</a><ul><li><a href="#t22.1 index.js">2.1 index.js</a></li><li><a href="#t32.2 createStore.js">2.2 createStore.js</a></li><li><a href="#t42.3 combineReducers.js">2.3 combineReducers.js</a></li><li><a href="#t52.4 bindActionCreators.js">2.4 bindActionCreators.js</a></li><li><a href="#t62.5 compose.js">2.5 compose.js</a></li><li><a href="#t72.6 applyMiddleware.js">2.6 applyMiddleware.js</a></li></ul></li><li><a href="#t83. react-redux">3. react-redux</a></li><li><a href="#t94. 中间件">4. 中间件</a><ul><li><a href="#t104.1 自己实现日志中间件">4.1 自己实现日志中间件</a></li><li><a href="#t114.2 实现logger中间件">4.2 实现logger中间件</a></li></ul></li><li><a href="#t125.counter">5.counter</a><ul><li><a href="#t135.1 Counter.js">5.1 Counter.js</a></li><li><a href="#t145.2 actions.js">5.2 actions.js</a></li><li><a href="#t155.3 reducers.js">5.3 reducers.js</a></li><li><a href="#t165.4 compose.js">5.4 compose.js</a></li><li><a href="#t175.5 applyMiddleware">5.5 applyMiddleware</a></li><li><a href="#t185.6 logger">5.6 logger</a></li><li><a href="#t195.7 thunk">5.7 thunk</a></li><li><a href="#t205.8 promise">5.8 promise</a></li><li><a href="#t215.9 join.js">5.9 join.js</a></li></ul></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. Redux&#x4E2D;&#x95F4;&#x4EF6;">1. Redux&#x4E2D;&#x95F4;&#x4EF6; <a href="#t01. Redux&#x4E2D;&#x95F4;&#x4EF6;"> # </a></h2>
<p><img src="http://img.zhufengpeixun.cn/reduxmiddleware.jpg" alt="reduxmiddleware"></p>
<h2 id="t12. redux">2. redux <a href="#t12. redux"> # </a></h2>
<h3 id="t22.1 index.js">2.1 index.js <a href="#t22.1 index.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> createStore <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./createStore&apos;</span>;
<span class="hljs-keyword">import</span> combineReducers <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./combineReducers&apos;</span>;
<span class="hljs-keyword">import</span> bindActionCreators <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./bindActionCreators&apos;</span>;
<span class="hljs-keyword">import</span> compose <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./compose&apos;</span>;
<span class="hljs-keyword">import</span> applyMiddleware <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./applyMiddleware&apos;</span>;
<span class="hljs-keyword">export</span> {
    createStore,
    combineReducers,
    bindActionCreators,
    compose,
    applyMiddleware
}
</code></pre>
<h3 id="t32.2 createStore.js">2.2 createStore.js <a href="#t32.2 createStore.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">reducer, preloadedState,enhancer</span>) </span>{
    <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> enhancer !== <span class="hljs-string">&apos;undefined&apos;</span>) {
       <span class="hljs-keyword">return</span> enhancer(createStore)(reducer, preloadedState)
    }
    <span class="hljs-keyword">let</span> state = preloadedState;
    <span class="hljs-keyword">let</span> listeners = [];
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getState</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-comment">// return state;</span>
        <span class="hljs-keyword">return</span> <span class="hljs-built_in">JSON</span>.parse(<span class="hljs-built_in">JSON</span>.stringify(state));
    }
    <span class="hljs-comment">//&#x6D3E;&#x53D1;&#x5206;&#x53D1;&#x7684;&#x610F;&#x601D;</span>
    <span class="hljs-comment">//action &#x52A8;&#x4F5C; &#x63CF;&#x8FF0;&#x4E00;&#x4E0B;&#x4F60;&#x60F3;&#x5E72;&#x4EC0;&#x4E48;,&#x52A8;&#x4F5C;&#x662F;&#x4E00;&#x4E2A;&#x666E;&#x901A;&#x7684;JS&#x5BF9;&#x8C61;&#xFF0C;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x5C5E;&#x6027;&#x662F;&#x5FC5;&#x987B;&#x7684;&#x3002;type,&#x5176;&#x5B83;&#x5C5E;&#x6027;&#x968F;&#x610F; </span>
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">dispatch</span>(<span class="hljs-params">action</span>) </span>{
        <span class="hljs-comment">//&#x63A5;&#x6536;&#x65B0;&#x7684;&#x52A8;&#x4F5C;&#x540E;&#xFF0C;&#x901A;&#x8FC7; &#x624D;&#x72B6;&#x6001; &#x548C;&#x65B0;&#x52A8;&#x4F5C;&#x8BA1;&#x7B97;&#x51FA;&#x65B0;&#x72B6;&#x6001;</span>
        state = reducer(state, action);
        <span class="hljs-comment">//&#x7136;&#x540E;&#x901A;&#x8FC7;&#x6240;&#x6709;&#x7684;&#x76D1;&#x542C;&#x51FD;&#x6570;&#x6267;&#x884C;</span>
        listeners.forEach(<span class="hljs-function"><span class="hljs-params">listener</span> =&gt;</span> listener());
    }
    <span class="hljs-comment">//&#x6D3E;&#x53D1;&#x4E86;&#x4E00;&#x4E2A;&#x52A8;&#x4F5C;&#x83B7;&#x53D6;&#x521D;&#x59CB;&#x503C;&#xFF0C;&#x5176;&#x5B9E;&#x5728;redux&#x5185;&#x90E8;&#x662F;&#x6D3E;&#x53D1;&#x4E00;&#x4E2A;INIT: &apos;@@redux/INIT&apos;&#x52A8;&#x4F5C;</span>
    dispatch({ <span class="hljs-attr">type</span>: <span class="hljs-string">&apos;@@redux/INIT&apos;</span> });
    <span class="hljs-comment">//&#x8BA2;&#x9605;&#xFF0C;&#x4F9B;&#x5916;&#x754C;&#x8BA2;&#x9605;&#x672C;&#x4ED3;&#x5E93;&#x4E2D;&#x72B6;&#x6001;&#x7684;&#x53D8;&#x5316; &#xFF0C;&#x5982;&#x679C;&#x72B6;&#x6001; &#x53D8;&#x5316; &#x4E86;&#x4F1A;&#x6267;&#x884C;&#x8BA2;&#x9605;&#x7684;&#x903B;&#x8F91; </span>
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">subscribe</span>(<span class="hljs-params">listener</span>) </span>{
        listeners.push(listener);
        <span class="hljs-comment">//&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x53D6;&#x6D88;&#x8BA2;&#x9605;&#x51FD;&#x6570;</span>
        <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
            listeners = listeners.filter(<span class="hljs-function"><span class="hljs-params">item</span> =&gt;</span> item != listener)
        }
    }
    <span class="hljs-keyword">return</span> {
        getState, dispatch, subscribe
    }
}
</code></pre>
<h3 id="t42.3 combineReducers.js">2.3 combineReducers.js <a href="#t42.3 combineReducers.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> reducers =&gt; <span class="hljs-function">(<span class="hljs-params">state = {}, action</span>) =&gt;</span> <span class="hljs-built_in">Object</span>.keys(reducers).reduce(<span class="hljs-function">(<span class="hljs-params">currentState, key</span>) =&gt;</span> {
    currentState[key] = reducers[key](state[key], action);
    <span class="hljs-keyword">return</span> currentState;
}, {});
</code></pre>
<h3 id="t52.4 bindActionCreators.js">2.4 bindActionCreators.js <a href="#t52.4 bindActionCreators.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">bindActionCreators</span>(<span class="hljs-params">actions,dispatch</span>)</span>{
    <span class="hljs-keyword">let</span> newActions = {};
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> attr <span class="hljs-keyword">in</span> actions){
     newActions[attr] = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
         dispatch(actions[attr].apply(<span class="hljs-literal">null</span>,<span class="hljs-built_in">arguments</span>));
     }
    }
    <span class="hljs-keyword">return</span> newActions;
 }
</code></pre>
<h3 id="t62.5 compose.js">2.5 compose.js <a href="#t62.5 compose.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">...funcs</span>)</span>{
    <span class="hljs-keyword">return</span> funcs.reduce(<span class="hljs-function">(<span class="hljs-params">a,b</span>)=&gt;</span><span class="hljs-function">(<span class="hljs-params">...args</span>)=&gt;</span>a(b(...args)));
}
</code></pre>
<h3 id="t72.6 applyMiddleware.js">2.6 applyMiddleware.js <a href="#t72.6 applyMiddleware.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> compose <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./compose&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">...middlewares</span>)</span>{
    <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">createStore</span>)</span>{
        <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">reducer</span>)</span>{
            <span class="hljs-keyword">let</span> store = createStore(reducer);
            <span class="hljs-keyword">let</span> dispatch;
            middlewares = middlewares.map(<span class="hljs-function"><span class="hljs-params">middleware</span>=&gt;</span>middleware({
                <span class="hljs-attr">getState</span>:store.getState,
                <span class="hljs-attr">dispatch</span>:<span class="hljs-function"><span class="hljs-params">action</span>=&gt;</span>dispatch(action)
            }));
            dispatch = compose(...middlewares)(store.dispatch);
            <span class="hljs-keyword">return</span> {...store,dispatch};
        }
    }
}
</code></pre>
<h2 id="t83. react-redux">3. react-redux <a href="#t83. react-redux"> # </a></h2>
<p>index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {bindActionCreators} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../redux&apos;</span>
<span class="hljs-keyword">let</span> Context = React.createContext();
<span class="hljs-comment">// Provider&#x7684;&#x4F5C;&#x7528;&#x5C31;&#x662F;&#x628A;store &#x5B9A;&#x7FA9;</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Provider</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  render() {
    <span class="hljs-keyword">let</span> store = <span class="hljs-keyword">this</span>.props.store;
    <span class="hljs-keyword">return</span> &lt;Context.Provider value={{ store }}&gt;
      {this.props.children}
    &lt;/Context.Provider&gt;
  }
}
// connect&#x662F;&#x4E00;&#x4E2A;&#x9AD8;&#x9636;&#x51FD;&#x6570; &#x8FD4;&#x56DE;&#x7684;&#x662F;&#x4E00;&#x4E2A;&#x7EC4;&#x4EF6;
let connect = (mapStateToProps,mapDispatchToProps) =&gt; (Component) =&gt; {
  // connect&#x4ED6;&#x662F;&#x4E00;&#x4E2A;&#x9AD8;&#x9636;&#x7EC4;&#x4EF6;
  class Proxy extends React.Component {
    // &#x628A;&#x5F53;&#x524D;&#x7528;&#x4F20;&#x9012;&#x7684;&#x51FD;&#x6570; &#x6267;&#x884C; &#x5E76;&#x4E14;&#x628A;&#x7ED3;&#x679C;&#x4F5C;&#x4E3A;&#x72B6;&#x6001;&#x653E;&#x5728;&#x5F53;&#x524D;&#x7684;&#x4EE3;&#x7406;&#x7EC4;&#x4EF6;&#x4E0A;&#x9762;
    state = mapStateToProps(this.props.store.getState());
    componentWillMount(){
      this.unsub = this.props.store.subscribe(()=&gt;{
        // &#x518D;&#x53BB;&#x8C03;&#x7528;&#x7528;&#x6237;&#x7684;&#x51FD;&#x6570;&#x6267;&#x884C; &#x628A;&#x6267;&#x884C;&#x7ED3;&#x679C; &#x4F5C;&#x4E3A;&#x65B0;&#x7684;&#x72B6;&#x6001;
        this.setState(mapStateToProps(this.props.store.getState()))
      })
    }
    componentWillUnmount(){
      this.unsub(); // &#x53D6;&#x6D88;&#x8BA2;&#x9605;
    }
    render(){
      let dispatchs 
      if (typeof mapDispatchToProps === &apos;function&apos;){
        dispatchs = mapDispatchToProps(this.props.store.dispatch);
      }else{
        // &#x5982;&#x679C;&#x7ED1;&#x5B9A;&#x7684;&#x662F;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x5C31;&#x8BA9;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x4F7F;&#x7528;bindActionCreatros&#x8FDB;&#x884C;&#x7ED1;&#x5B9A;
        dispatchs = bindActionCreators(mapDispatchToProps,this.props.store.dispatch);
      }
      return &lt;Component {...this.state} {...dispatchs}&gt;&lt;/Component&gt;
    }
  }
  return ()=&gt;{
    return &lt;Context.Consumer&gt;
      {({ store }) =&gt; {
        return &lt;Proxy store={store}&gt;&lt;/Proxy&gt;  
      }}
    &lt;/Context.Consumer&gt;
  }
}
export { Provider ,connect};
</code></pre>
<h2 id="t94. &#x4E2D;&#x95F4;&#x4EF6;">4. &#x4E2D;&#x95F4;&#x4EF6; <a href="#t94. &#x4E2D;&#x95F4;&#x4EF6;"> # </a></h2>
<h3 id="t104.1 &#x81EA;&#x5DF1;&#x5B9E;&#x73B0;&#x65E5;&#x5FD7;&#x4E2D;&#x95F4;&#x4EF6;">4.1 &#x81EA;&#x5DF1;&#x5B9E;&#x73B0;&#x65E5;&#x5FD7;&#x4E2D;&#x95F4;&#x4EF6; <a href="#t104.1 &#x81EA;&#x5DF1;&#x5B9E;&#x73B0;&#x65E5;&#x5FD7;&#x4E2D;&#x95F4;&#x4EF6;"> # </a></h3>
<p>&#x6211;&#x4EEC;&#x6539;&#x5199;&#x4E86;&#xFF0C;dispatch&#x65B9;&#x6CD5;&#x5B9E;&#x73B0;&#x4E86;&#x5728;&#x66F4;&#x6539;&#x72B6;&#x6001;&#x65F6;&#x6253;&#x5370;&#x524D;&#x540E;&#x7684;&#x72B6;&#x6001;,&#x4F46;&#x662F;&#x8FD9;&#x79CD;&#x65B9;&#x6848;&#x5E76;&#x4E0D;&#x597D;&#x3002;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x91C7;&#x7528;&#x4E2D;&#x95F4;&#x7684;&#x65B9;&#x5F0F;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> store = createStore(reducer);
<span class="hljs-keyword">let</span> dispatch = store.dispatch;
store.dispatch = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">action</span>) </span>{
  <span class="hljs-built_in">console</span>.log(store.getState().number);
  dispatch(action);
  <span class="hljs-built_in">console</span>.log(store.getState().number)
};
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> store;
</code></pre>
<h3 id="t114.2 &#x5B9E;&#x73B0;logger&#x4E2D;&#x95F4;&#x4EF6;">4.2 &#x5B9E;&#x73B0;logger&#x4E2D;&#x95F4;&#x4EF6; <a href="#t114.2 &#x5B9E;&#x73B0;logger&#x4E2D;&#x95F4;&#x4EF6;"> # </a></h3>
<p>&#x4E2D;&#x95F4;&#x4EF6;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x5BF9;store.dispatch&#x65B9;&#x6CD5;&#x8FDB;&#x884C;&#x4E86;&#x6539;&#x9020;&#xFF0C;&#x5728;&#x53D1;&#x51FA; Action &#x548C;&#x6267;&#x884C; Reducer &#x8FD9;&#x4E24;&#x6B65;&#x4E4B;&#x95F4;&#xFF0C;&#x6DFB;&#x52A0;&#x4E86;&#x5176;&#x4ED6;&#x529F;&#x80FD;</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> logger = <span class="hljs-function"><span class="hljs-params">store</span> =&gt;</span> dispatch =&gt; <span class="hljs-function"><span class="hljs-params">action</span>=&gt;</span>{
  <span class="hljs-built_in">console</span>.log(store.getState().number);
  dispatch(action);
  <span class="hljs-built_in">console</span>.log(store.getState().number)
};
<span class="hljs-keyword">let</span> applyMiddleWare = <span class="hljs-function"><span class="hljs-params">middleware</span> =&gt;</span> createStore =&gt; <span class="hljs-function"><span class="hljs-params">reducer</span> =&gt;</span>{
  <span class="hljs-keyword">let</span> store = createStore(reducer);
  <span class="hljs-keyword">let</span> middle = middleware(store);
  <span class="hljs-keyword">let</span> dispatch = middle(store.dispatch);
  <span class="hljs-keyword">return</span> { <span class="hljs-comment">//&#x5C06;&#x4E2D;&#x95F4;&#x8FD4;&#x56DE;&#x7684;dispatch&#x65B9;&#x6CD5;&#x8986;&#x76D6;&#x6389;&#x539F;&#x6709;store&#x4E2D;&#x7684;dispatch</span>
    ...store,
    dispatch
  }
};
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> applyMiddleWare(logger)(createStore)(reducer);
</code></pre>
<h2 id="t125.counter">5.counter <a href="#t125.counter"> # </a></h2>
<h3 id="t135.1 Counter.js">5.1 Counter.js <a href="#t135.1 Counter.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React, { Component } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> store <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../store&apos;</span>;
<span class="hljs-keyword">import</span> actions <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../store/actions/counter&apos;</span>;
<span class="hljs-keyword">import</span> {connect} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../react-redux&apos;</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Counter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span> </span>{
    <span class="hljs-keyword">constructor</span>(props){
        <span class="hljs-keyword">super</span>(props);
    }
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{this.props.number}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{this.props.increment}</span> &gt;</span>+<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{this.props.thunkIncrement}</span> &gt;</span>thunk+<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{this.props.promiseIncrement}</span> &gt;</span>promise+<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{this.props.payloadIncrement}</span> &gt;</span>payload+<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> connect(
    <span class="hljs-function"><span class="hljs-params">state</span>=&gt;</span>state,
    actions
)(Counter);
</code></pre>
<h3 id="t145.2 actions.js">5.2 actions.js <a href="#t145.2 actions.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> types <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../action-types&apos;</span>;
<span class="hljs-comment">//actionCreator &#x521B;&#x5EFA;action&#x7684;&#x51FD;&#x6570;</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
    increment(){
        <span class="hljs-keyword">return</span> {<span class="hljs-attr">type</span>:types.INCREMENT,<span class="hljs-attr">payload</span>:<span class="hljs-number">1</span>}
    },
    thunkIncrement(){
        <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">dispatch,getState</span>)</span>{
            setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
                dispatch({<span class="hljs-attr">type</span>:types.INCREMENT,<span class="hljs-attr">payload</span>:<span class="hljs-number">1</span>});
            },<span class="hljs-number">1000</span>);
        }
    },
    promiseIncrement(){
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">resolve</span>)</span>{
            setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
                resolve({<span class="hljs-attr">type</span>:types.INCREMENT,<span class="hljs-attr">payload</span>:<span class="hljs-number">1</span>});
            },<span class="hljs-number">1000</span>);
        });
    },
    payloadIncrement(){
       <span class="hljs-keyword">return</span> {
           <span class="hljs-attr">type</span>:types.INCREMENT,
           <span class="hljs-attr">payload</span>:<span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">resolve,reject</span>)</span>{
               setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
                   <span class="hljs-keyword">if</span>(<span class="hljs-built_in">Math</span>.random()&gt;<span class="hljs-number">.5</span>){
                       resolve(<span class="hljs-number">1</span>);
                   }<span class="hljs-keyword">else</span>{
                       reject(<span class="hljs-number">-1</span>);
                   }
               },<span class="hljs-number">1000</span>);
           })
       }
    }
}
</code></pre>
<h3 id="t155.3 reducers.js">5.3 reducers.js <a href="#t155.3 reducers.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> types <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../action-types&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">state={number:<span class="hljs-number">0</span>},action</span>)</span>{
    <span class="hljs-keyword">switch</span>(action.type){
        <span class="hljs-keyword">case</span> types.INCREMENT:
            <span class="hljs-keyword">return</span> {<span class="hljs-attr">number</span>:state.number + (action.payload)};
        <span class="hljs-keyword">default</span>:
            <span class="hljs-keyword">return</span> state;
    }
}
</code></pre>
<p>action-types.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> INCREMENT = <span class="hljs-string">&apos;INCREMENT&apos;</span>;
</code></pre>
<h3 id="t165.4 compose.js">5.4 compose.js <a href="#t165.4 compose.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">add1</span>(<span class="hljs-params">str</span>)</span>{
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;1&apos;</span>+str;
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">add2</span>(<span class="hljs-params">str</span>)</span>{
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;2&apos;</span>+str;
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">add3</span>(<span class="hljs-params">str</span>)</span>{
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;3&apos;</span>+str;
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">compose1</span>(<span class="hljs-params">...fns</span>)</span>{
  <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">...args</span>)</span>{
    <span class="hljs-keyword">let</span> last = fns.pop();
    <span class="hljs-keyword">return</span> fns.reduceRight(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">val,fn</span>)</span>{
        <span class="hljs-keyword">return</span> fn(val);
    },last(...args))
  }
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">compose2</span>(<span class="hljs-params">...fns</span>)</span>{
    <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">...args</span>)</span>{
       <span class="hljs-keyword">return</span> fns.reduce(<span class="hljs-function">(<span class="hljs-params">a,b</span>)=&gt;</span>a(b(...args)));
    }
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">compose3</span>(<span class="hljs-params">...funcs</span>)</span>{
    <span class="hljs-keyword">return</span> funcs.reduce(<span class="hljs-function">(<span class="hljs-params">a,b</span>)=&gt;</span><span class="hljs-function">(<span class="hljs-params">...args</span>)=&gt;</span>a(b(...args)));
}

<span class="hljs-keyword">let</span> result = compose3(add3,add2,add1)(<span class="hljs-string">&apos;zfpx&apos;</span>);
<span class="hljs-built_in">console</span>.log(result);
</code></pre>
<h3 id="t175.5 applyMiddleware">5.5 applyMiddleware <a href="#t175.5 applyMiddleware"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> applyMiddleware = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">middleware</span>)</span>{
    <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">createStore</span>)</span>{
        <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">reducer</span>)</span>{
            <span class="hljs-keyword">let</span> store = createStore(reducer);
            middleware = middleware(store);
            <span class="hljs-keyword">let</span> dispatch = middleware(store.dispatch);
            <span class="hljs-keyword">return</span> {...store,dispatch}
        }
    }
}
</code></pre>
<h3 id="t185.6 logger">5.6 logger <a href="#t185.6 logger"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> logger = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">store</span>)</span>{
    <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">next</span>)</span>{
        <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">action</span>)</span>{
            <span class="hljs-built_in">console</span>.log(store.getState());
            next(action);
            <span class="hljs-built_in">console</span>.log(store.getState());
        }
    }
}
</code></pre>
<h3 id="t195.7 thunk">5.7 thunk <a href="#t195.7 thunk"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> thunk = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">{getState,dispatch}</span>)</span>{
    <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">next</span>)</span>{
        <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">action</span>)</span>{
           <span class="hljs-keyword">if</span>(<span class="hljs-keyword">typeof</span> action == <span class="hljs-string">&apos;function&apos;</span>){
                action(dispatch,getState);
           }<span class="hljs-keyword">else</span>{
               next(action);
           }
        }
    }
}
</code></pre>
<h3 id="t205.8 promise">5.8 promise <a href="#t205.8 promise"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> promise = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">{getState,dispatch}</span>)</span>{
    <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">next</span>)</span>{
        <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">action</span>)</span>{
           <span class="hljs-keyword">if</span>(action.then){
               action.then(dispatch);
           }<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(action.payload &amp;&amp; action.payload.then){
               action.payload.then(<span class="hljs-function"><span class="hljs-params">payload</span>=&gt;</span>dispatch({...action,payload}),payload=&gt;dispatch({...action,payload}));
           }<span class="hljs-keyword">else</span>{
               next(action);
           }
        }
    }
}
</code></pre>
<h3 id="t215.9 join.js">5.9 join.js <a href="#t215.9 join.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {createStore} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux&apos;</span>;
<span class="hljs-keyword">import</span> reducers <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./reducers&apos;</span>;
<span class="hljs-keyword">let</span> logger = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">store</span>)</span>{
    <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">next</span>)</span>{
        <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">action</span>)</span>{
            <span class="hljs-built_in">console</span>.log(store.getState());
            next(action);
            <span class="hljs-built_in">console</span>.log(store.getState());
        }
    }
}
<span class="hljs-keyword">let</span> thunk = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">{getState,dispatch}</span>)</span>{
    <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">next</span>)</span>{
        <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">action</span>)</span>{
           <span class="hljs-keyword">if</span>(<span class="hljs-keyword">typeof</span> action == <span class="hljs-string">&apos;function&apos;</span>){
                action(dispatch,getState);
           }<span class="hljs-keyword">else</span>{
               next(action);
           }
        }
    }
}
<span class="hljs-keyword">let</span> promise = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">{getState,dispatch}</span>)</span>{
    <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">next</span>)</span>{
        <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">action</span>)</span>{
           <span class="hljs-keyword">if</span>(action.then){
               action.then(dispatch);
           }<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(action.payload &amp;&amp; action.payload.then){
               action.payload.then(<span class="hljs-function"><span class="hljs-params">payload</span>=&gt;</span>dispatch({...action,payload}),payload=&gt;dispatch({...action,payload}));
           }<span class="hljs-keyword">else</span>{
               next(action);
           }
        }
    }
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">compose</span>(<span class="hljs-params">...funcs</span>)</span>{
    <span class="hljs-keyword">return</span> funcs.reduce(<span class="hljs-function">(<span class="hljs-params">a,b</span>)=&gt;</span><span class="hljs-function">(<span class="hljs-params">...args</span>)=&gt;</span>a(b(...args)));
}

<span class="hljs-keyword">let</span> applyMiddleware = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">...middlewares</span>)</span>{
    <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">createStore</span>)</span>{
        <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">reducer</span>)</span>{
            <span class="hljs-keyword">let</span> store = createStore(reducer);
            <span class="hljs-keyword">let</span> dispatch;
            middlewares = middlewares.map(<span class="hljs-function"><span class="hljs-params">middleware</span>=&gt;</span>middleware({
                <span class="hljs-attr">getState</span>:store.getState,
                <span class="hljs-attr">dispatch</span>:<span class="hljs-function"><span class="hljs-params">action</span>=&gt;</span>dispatch(action)
            }));
            dispatch = compose(...middlewares)(store.dispatch);
            <span class="hljs-keyword">return</span> {...store,dispatch};
        }
    }
}
<span class="hljs-keyword">let</span> store = applyMiddleware(thunk,promise,logger)(createStore)(reducers);
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> store;
</code></pre>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
